﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CoachingOrganizer.Logic.Models.Tools.EmotionsPack;

namespace CoachingOrganizer.Android.Adapters.Tools.EmotionsPack
{
    public class CamerasAdapter : BaseAdapter
    {
        private class CameraInfoInternal
        {
            private readonly int _number;
            private readonly string _name;

            public int Number
            {
                get { return _number; }
            }

            public string Name
            {
                get { return _name; }
            }

            public CameraInfoInternal(int number, string nameTemplate, string name = null)
            {
                _number = number;
                _name = name ?? string.Format(nameTemplate, number);
            }
        }

        private readonly List<CameraInfoInternal> _items = new List<CameraInfoInternal>();
        private readonly Activity _context;
        private readonly EmotionsTraceModel _model;

        public CamerasAdapter(Activity context, EmotionsTraceModel model)
        {
            _context = context;
            _model = model;
            for (int i = 0; i < global:: Android.Hardware.Camera.NumberOfCameras; i++)
                switch (i)
                {
                    case (int)global::Android.Hardware.CameraFacing.Back:
                        _items.Add(new CameraInfoInternal(i, null, context.Resources.GetString(Resource.String.BackCameraName)));
                        break;
                    case (int)global::Android.Hardware.CameraFacing.Front:
                        _items.Add(new CameraInfoInternal(i, null, context.Resources.GetString(Resource.String.FrontCameraName)));
                        break;
                    default:
                        _items.Add(new CameraInfoInternal(i, context.Resources.GetString(Resource.String.CameraNameTemplate)));
                        break;
                }
            global::Android.Hardware.Camera.CameraInfo info = new global::Android.Hardware.Camera.CameraInfo();
            global::   Android.Hardware.Camera.GetCameraInfo(0, info);
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return position;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return _items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = _context.LayoutInflater.Inflate(Resource.Layout.CameraInfoLayout, parent, false);
            HookModelToView(view, _items[position]);
            return view;
        }

        private void HookModelToView(View view, CameraInfoInternal cameraInfo)
        {
            TextView cameraNameTextView = view.FindViewById<TextView>(Resource.Id.cameraNameTextView);
            cameraNameTextView.Text = cameraInfo.Name;
        }
    }
}